require.config({
	paths : {
		"jquery" : "jquery",
		"common" : "common",
		"magnifier" : "magnifier",
		"cookie" : "cookie"
	}
})

require(["jquery","common","magnifier"],function($,common,magnifier,cookie){
	$("#header").load("head.html",function(){common.fn();});
	$("#footer").load("foot.html",function(){common.fn();});
	$(function(){
		magnifier.fn();//放大镜+轮播
		
		/*选择样式*/
		$(".spec_img u a").each(function(index,value){
			$(value).click(function(){
				console.log("123");
				$(this).addClass("choice").siblings().removeClass("choice");
			})
		});
		$(".spec_size u a").each(function(index,value){
			$(value).click(function(){
				$(this).addClass("choice").siblings().removeClass("choice");
			})
		});
		
		
		/*选项卡1*/
		$(".conter_option1").click(function(){
			$(this).addClass("choice").siblings().removeClass("choice");
			$(".con-detail").css("display","block").siblings().css("display","none");
		});
		$(".conter_option2").click(function(){
			$(this).addClass("choice").siblings().removeClass("choice");
			$(".con-comment").css("display","block").siblings().css("display","none");
		});
		$(".conter_option3").click(function(){
			$(this).addClass("choice").siblings().removeClass("choice");
			$(".con-consult").css("display","block").siblings().css("display","none");
		});
		
		/*选项卡2*/
		$(".consult-xcho1").click(function(){
			$(this).addClass("consult-xcho-act").siblings().removeClass("consult-xcho-act");
			$(".consult-x1").css("display","block").siblings().css("display","none");
		});
		$(".consult-xcho2").click(function(){
			$(this).addClass("consult-xcho-act").siblings().removeClass("consult-xcho-act");
			$(".consult-x2").css("display","block").siblings().css("display","none");
		});
		$(".consult-xcho3").click(function(){
			$(this).addClass("consult-xcho-act").siblings().removeClass("consult-xcho-act");
			$(".consult-x3").css("display","block").siblings().css("display","none");
		});
		$(".consult-xcho4").click(function(){
			$(this).addClass("consult-xcho-act").siblings().removeClass("consult-xcho-act");
			$(".consult-x4").css("display","block").siblings().css("display","none");
		});
		
		
		$(".action_add").click(function(){/*添加购物车*/
			if(getCookie("active_user")){
				var $cookier = getCookie('users_' + getCookie("active_user"));
				var $product = $cookier.split("-");
				var offon = true , $cookieValue , $cookieKey;
				if($product != ""){
					for(var j = 0;j < $product.length;j ++){
						var show = JSON.parse($product[j]);
						if(show.id == $(this).attr("name")){
							var sew = parseInt(show.num) + 1;
							$(".action_add").attr("num", sew );
							$cookieValue = '{"id":"' + $(".action_add").attr("name") + '","price":"' + $(".action_add").attr("pri") + '","num":"' + $(".action_add").attr("num") + '","img":"' + $(".action_add").attr("img") + '"}';
							$cookieKey = 'users_' + getCookie("active_user");
							$product.splice(j,1);
							if($product.length == 0){
								setCookie($cookieKey , $cookieValue,7);
							}else{
								setCookie($cookieKey , $product.join("-") + "-" + $cookieValue,7);
							}
							offon = false;
							break;
						}
					}
					if(offon){
						var sew = parseInt($(".action_add").attr("num")) + 1;
						$(".action_add").attr("num", sew );
						var $cookieValue = '{"id":"' + $(".action_add").attr("name") + '","price":"' + $(".action_add").attr("pri") + '","num":"' + $(".action_add").attr("num") + '","img":"' + $(".action_add").attr("img") + '"}';
						var $cookieKey = 'users_' + getCookie("active_user");
						setCookie($cookieKey , $cookier + "-" + $cookieValue,7);
					}	
				}else{
					var sew = parseInt($(".action_add").attr("num")) + 1;
					$(".action_add").attr("num", sew );
					$cookieValue = '{"id":"' + $(".action_add").attr("name") + '","price":"' + $(".action_add").attr("pri") + '","num":"' + $(".action_add").attr("num") + '","img":"' + $(".action_add").attr("img") + '"}';
					$cookieKey = 'users_' + getCookie("active_user");
					setCookie($cookieKey , $cookier + $cookieValue,7);
				}
				alert("添加成功！");
				alert(getCookie('users_' + getCookie("active_user")));
				
				var $buycar = 0;/*添加当前用户购物车数量*/
				var $productnum = getCookie('users_' + getCookie("active_user")).split("-");
				if($productnum != ""){
					for(var j = 0;j < $productnum.length;j ++){
						var show = JSON.parse($productnum[j]);
						$buycar += parseInt(show.num);
					}	
				}
				$(".buycarnum").text($buycar);
				
			}else{
				alert("请先登录！");
			}
		});
		
	});
	
})